package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.new
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UseConsumableResp
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.resp.back_resp.UserBackpackDecorationResp
import com.polaris.live.resp.back_resp.UserPartyBackgroundListResp
import com.polaris.live.resp.go_resp.UseConsumableRequest
import com.polaris.live.resp.go_resp.UserPartyBackgroundUseRequest
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope

/**
 * RoomBackgroundViewModel
 *
 * @author Created by 半仙 on 2024/12/18/018 19:36
 */
class RoomBackgroundViewModel : BaseViewModel() {

    //获取自定义派对背景列表
    val mDiyBackgroundLiveData = MutableLiveData<ResultState<UserPartyBackgroundListResp>>()
    fun getBackgroundDiy() {
        request({ userService.getPartyDiyBackground() }, mDiyBackgroundLiveData)
    }

    //获取道具
    val mDecorationLiveData = MutableLiveData<ResultState<List<UserBackpackConsumableResp>>>()
    fun getConsumable() {
        request({ userService.getConsumableList() }, mDecorationLiveData)
    }

    //使用道具
    val mConsumableLiveData = MutableLiveData<ResultState<UseConsumableResp>>()
    fun useConsumable(backpackId: Long, position: Int, mAnchorId: Long? = null) {
        val useDressUp = UseConsumableRequest(backpackId, null, mAnchorId, position.toLong())
        requestLoading({ userService.useConsumable(useDressUp) }, mConsumableLiveData)
    }

    //使用自定义派对位置
    val mUsePartyLiveData = MutableLiveData<Int>()
    fun usePartyPosition(position: Int, useStatus: Int) {
        requestLoading(
            { userService.usePartyBackground(UserPartyBackgroundUseRequest(position, useStatus)) },
            {
                mUsePartyLiveData.value = useStatus
            }, {
                ToastGravity.showGravity(it.errorMsg)
            }
        )
    }

    //卸下自定义派对位置
    fun unMountPartyPosition(position: Int) {
        requestLoading(
            { userService.usePartyBackground(UserPartyBackgroundUseRequest(position, AppConst.NO)) },
            {}
        )
    }

    //获取派对游戏列表和背包列表
    val mDiyAndBackLiveData = MutableLiveData<Pair<UserPartyBackgroundListResp?, List<UserBackpackDecorationResp>?>>()
    fun getDiyAndBackpack() {
        request({
            coroutineScope {
                val listResp =
                    async { userService.getPartyDiyBackground() }.await()
                val backResp =
                    async { userService.getUserBackpackByType(CommonConst.DressUp.PARTY_BACKGROUND.toInt(), 0) }.await()

                val pair = Pair(listResp.result, backResp.result)
                listResp.new(pair)
            }
        }, {
            mDiyAndBackLiveData.value = it
        }, {
            mDiyAndBackLiveData.value = Pair(null, null)
        })
    }
}